<template>
  <div>
    <!--搜索栏-->
    <el-card id="search">
      <el-row>
        <el-col :span="20">
          <el-input v-model="searchModel.name" placeholder="请输入员工姓名" clearable/>
          <el-button type="primary" round icon="el-icon-search" @click="getWagesList">查询</el-button>
        </el-col>
        <el-col :span="4" align="right">
          <el-button type="primary" icon="el-icon-plus" circle @click="openAddUI(null)"/>
        </el-col>
      </el-row>
    </el-card>
    <!--结果，分页-->
    <el-card>
      <template>
        <el-table :data="wagesList" stripe style="width: 100%">
          <el-table-column prop="id" label="编号" width="180"/>
          <el-table-column prop="userId" label="员工编号" width="180"/>
          <el-table-column prop="userName" label="员工姓名" width="180"/>
          <el-table-column prop="month" label="月份" width="180"/>
          <el-table-column prop="positionWages" label="基础工资" width="180"/>
          <el-table-column prop="performance" label="绩效工资" width="180"/>
          <el-table-column prop="subsidy" label="补贴" width="180"/>
          <el-table-column prop="status" label="状态" width="180">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status == 1" type="info">待审批</el-tag>
              <el-tag v-else-if="scope.row.status == 2" type="success">已通过</el-tag>
              <el-tag v-else type="danger">驳回</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="180">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit" size="mini" circle @click="openAddUI(scope.row.id)"/>
              <el-button type="danger" icon="el-icon-delete" size="mini" circle @click="deleteWages(scope.row)"/>
            </template>
          </el-table-column>
          <el-table-column prop="salary" label="总计"/>
          <el-table-column prop="notes" label="备注"/>
        </el-table>
      </template>
    </el-card>
    <!--分页组件-->
    <el-card>
      <el-pagination
        :current-page="searchModel.page"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="searchModel.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <!--用户信息编辑对话框-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clearForm">
      <el-form ref="wagesForm" :model="wagesForm">
        <!--        <el-form-item label="密码" prop="password" :label-width="formLabelWidth">-->
        <!--          <el-input v-model="userForm.password" type="password" autocomplete="off">-->
        <!--        </el-form-item>-->
        <el-form-item label="员工" :label-width="formLabelWidth">
          <template>
            <el-select v-model="wagesForm.userId" placeholder="请选择">
              <el-option v-for="(item,index) in userList" :key="index" :label="item.username" :value="item.id"/>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="工资月份" :label-width="formLabelWidth">
          <el-input v-model="wagesForm.month" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="绩效工资" :label-width="formLabelWidth">
          <el-input v-model="wagesForm.performance" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="补贴工资" :label-width="formLabelWidth">
          <el-input v-model="wagesForm.subsidy" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="wagesForm.notes" autocomplete="off"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveWages">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import wagesApi from '@/api/wages'

export default {
  data() {
    return {
      formLabelWidth: '130px',
      wagesForm: {},
      dialogFormVisible: false,
      title: '',
      total: 0,
      searchModel: {
        page: 1,
        pageSize: 10
      },
      wagesList: [],
      userList: []

    }
  },
  created() {
    this.getWagesList()
  },
  methods: {
    deleteWages(wages) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        wagesApi.deleteWages(wages.id).then(res => {
          this.$message({
            type: 'success',
            message: res.msg
          })
          this.getWagesList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    saveWages() {
      // 触发表单验证
      this.$refs['wagesForm'].validate((valid) => {
        if (valid) {
          // 提交请求给后台
          wagesApi.saveWages(this.wagesForm).then(res => {
            // 成功提示，关闭对话框，刷新数据
            this.$message({
              message: res.msg,
              type: 'success'
            })
            this.dialogFormVisible = false
            this.getWagesList()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    clearForm() {
      this.wagesForm = {}
      this.$refs.wagesForm.clearValidate()
    },
    openAddUI(id) {
      this.dialogFormVisible = true
      wagesApi.getUserName({'status': 1}).then(res => {
        this.userList = res.data
      })
      if (id == null) {
        this.title = '新增用户工资信息'
      } else {
        this.title = '修改用户工资信息'
        wagesApi.getWagesById(id).then(res => {
          this.wagesForm = res.data
        })
      }
    },
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize
      this.getWagesList()
    },
    handleCurrentChange(page) {
      this.searchModel.page = page
      this.getWagesList()
    },
    getWagesList() {
      wagesApi.getWagesList(this.searchModel).then(res => {
        this.wagesList = res.data.rows
        this.total = res.data.total
      })
    }
  }
}
</script>

<style>
#search .el-input {
  width: 200px;
  margin-right: 10px;
}

.el-dialog .el-input {
  width: 85%;
}
</style>
